<?php

namespace App\Console\Commands;

use App\Library\Logic\SmsLogic;
use Illuminate\Console\Command;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Mrgoon\AliSms\AliSms;

class SendSmsMessageCommand extends Command
{
    /**
     * 发送开始拍卖时时消息
     *
     * @var string
     */
    protected $signature = 'command:sendSmsMessage {content}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     *
     * @return void
     *
     */
    public function handle()
    {
        if (is_null($this->argument('content'))) return;
//        $content = json_decode($this->argument('content'), true);
        $content = $this->argument('content');
        Log::debug('test---'.var_export($content, true));
        die;
        $sms = config('sms');
        $mobile = $content['content']['mobile'];
        if ($content['content']['type'] === 0) {//验证码
            $template = $content['content']['template'];
            $content = ['code' => $content['content']['ext']['code']];
        } else if ($content['content']['type'] === 1) {//出价失败
            $template = $content['content']['template'];
            $content = ['name' => $content['content']['ext']['name']];
        } else if ($content['content']['type'] === 2) {//出价成功
            $template = $content['content']['template'];
            $content = [
                'name' => $content['content']['ext']['name'],
                'money' => $content['content']['ext']['money']
            ];
        } else if ($content['content']['type'] === 3) {//新商品
            $template = $content['content']['template'];
            $content = ['name' => $content['content']['ext']['product_name']];
        } else if ($content['content']['type'] === 4) {//开拍倒计时
            $template = $content['content']['template'];
            $content = ['name' => $content['content']['ext']['product_name']];
        }

//        if (prod()) {
        $aliSms = new AliSms;
        $response = $aliSms->sendSms($mobile, $template, $content, [
            'access_key' => $sms['key'],
            'access_secret' => $sms['secret'],
            'sign_name' => $sms['name'],
        ]);
//        } else {
//            $response = new stdClass;
//            $response->Code = 'OK';
//        }

        SmsLogic::insertForModel([
            'code' => $code ?? null,
            'content' => json_encode($content),
            'ip' => request()->ip(),
            'result' => $response,
            'status' => $response->Code,
        ]);
    }
}
